package com.woniu.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.woniu.dao.AdminMapper;
import com.woniu.entity.Admin;
import com.woniu.entity.User;
import com.woniu.utils.MyPage;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Transactional
@Service
public class AdminService {
	@Autowired
	private AdminMapper adminMapper;

	public AdminMapper geAdminMapper() {
		return adminMapper;
	}

	/**
	 * @return 管理员首页获取所有用户信息，用于展示用户数据
	 */
	public List<User> getUserList() {
		// TODO Auto-generated method stub
		return adminMapper.getUserList();
	}

	/**
	 * @param name
	 * @param password
	 * @param phone
	 * @param role
	 * @return
	 */
	public Admin selectAdmin(String name, String password, String phone, String role) {
		// TODO Auto-generated method stub
		return adminMapper.selectAdmin(name, password, phone, role);
	}

	/**
	 * @param name
	 * @param phone
	 * @return
	 */
	public int selectAdmin(String name, String phone) {
		// TODO Auto-generated method stub
		return adminMapper.selectForgetAdmin(name, phone);
	}

	/**
	 * @param name
	 * @param phone
	 * @param newPassTwo
	 * @return
	 */
	public int updateForgetPass(String name, String phone, String newPassTwo) {
		// TODO Auto-generated method stub
		return adminMapper.updataForgetPass(name, phone, newPassTwo);
	}

	/**
	 * @return
	 */
	public List<Admin> getAdminList() {
		// TODO Auto-generated method stub
		return adminMapper.selectAll();
	}

	/**
	 * @param absolutePath
	 * @return
	 */
	public int updateAdminHeadImg(String absolutePath, String adminId) {
		// TODO Auto-generated method stub
		return adminMapper.updateAdminHeadImg(absolutePath, adminId);
	}

	/**
	 * @param pass
	 * @param adminid
	 * @return
	 */
	public int updateAdminInfo(String pass, String adminid) {
		// TODO Auto-generated method stub
		return adminMapper.updateAdminInfo(pass, adminid);
	}

	// criteria查询
	// 相当于已经把函数功能给你封装到dao层了，直接再service层用这些函数就可以了
	public MyPage<?> adminSelectByCond(String Name, String Phone, int pageNum, int pageSize) {

		// ?是占位符，不确定返回什么类型
		MyPage<?> page = null;

		// 创建where条件
		Example exm = new Example(Admin.class);
		Criteria crt = exm.createCriteria();
		if (!"".equals(Name))
			crt.andCondition("adminName LIKE", "%" + Name + "%"); // 模糊查询
		if (!"".equals(Phone))
			crt.andCondition("adminPhone LIKE", "%" + Phone + "%");

		// 查询
		// 总条数要在拦截之前算出来
		int count = adminMapper.selectCountByExample(exm);
		// [jqPaginator] totalCounts or totalPages is required
		// 分页插件要求必须有conuts或总页数
		if (count == 0)
			count = 1;
		// PageHelper.startPage(pageNum, pageSize); 就是拦截 相当于limit();
		PageHelper.startPage(pageNum, pageSize);
		// 符合条件的当前页要显示的数据集合list1
		List<Admin> list = adminMapper.selectByExample(exm);

		// 把符合条件的对象总数目、每页显示条目、当前页要显示的数据集合封装进自定义的工具类
		page = new MyPage<Admin>(count, pageSize, list);
		return page;

	}

	// 新建账户
	public String createAdmin(Admin admin) {
		int i = adminMapper.insert(admin);
		String str = (i == 0) ? "false" : "true";
		return str;
	}

	// 用户名唯一性验证
	public Admin selectAdminByName(String aName) {

		List<Admin> list = null;
		Example exm = new Example(Admin.class);
		Criteria crt = exm.createCriteria();
		crt.andCondition("adminName=", aName); // 精确查询

		list = adminMapper.selectByExample(exm);

		// -- 集合没有元素用isempty()，不是用==null
		if (list.isEmpty())
			return null;
		else
			return list.get(0);
	}

	// 删除账户
	public int deleteAdminById(int aId) {
		return adminMapper.deleteByPrimaryKey(aId);
	}

	// 修改账户信息
	public int updateAdmin(Admin admin) {

		Example exm = new Example(Admin.class);
		Criteria crt = exm.createCriteria();
		crt.andCondition("adminId=", admin.getAdminid()); // 精确查询
		return adminMapper.updateByExample(admin, exm);
	}

	// 根据账号密码获取用户
	public Admin selectByadminNameAndadminPass(String adminName, String adminPass) {

		List<Admin> list = null;
		Example exm = new Example(Admin.class);
		Criteria crt = exm.createCriteria();
		crt.andCondition("adminName=", adminName); // 精确查询
		crt.andCondition("adminPass=", adminPass); // 精确查询
		list = adminMapper.selectByExample(exm);
		// -- 集合没有元素用isempty()，不是用==null
		if (list.isEmpty())
			return null;
		else
			return list.get(0);
	}

	public Admin selectByadminPass1(String adminName) {
		Admin Admin = new Admin();
		Admin.setAdminname(adminName);
		return adminMapper.selectOne(Admin);
	}

	// 修改密码
	@Transactional
	public void updateAdminPass(String adminPass, String adminName) {
		adminMapper.updateAdminPass(adminPass, adminName);
	}

	// 根据用户名查询管理员：Shiro用
	public Admin selectAdminByShiro(String username) {
		// TODO Auto-generated method stub
		return adminMapper.selectAdminByShiro(username);
	}

}